package com.theatre.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theatre.userservice.mapper.PublisherMapper;
import com.theatre.userservice.mapper.ScriptMapper;
import com.theatre.userservice.model.entity.Publisher;
import com.theatre.userservice.model.entity.Script;
import com.theatre.userservice.model.vo.ScriptVO;
import com.theatre.userservice.service.PublisherService;
import com.theatre.userservice.service.ScriptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class PublisherServiceImpl extends ServiceImpl<PublisherMapper, Publisher> implements PublisherService {
    @Autowired
    private ScriptMapper scriptMapper;
    @Override
    public List<ScriptVO> selectScriptByPublisherId(Long publisherId) {
        // 1. 查询剧本列表
        List<Script> scripts = scriptMapper.selectList(
                new LambdaQueryWrapper<Script>()
                        .eq(Script::getPublisherId, publisherId)
                        .orderByAsc(Script::getStatus) // 根据状态排序，0/1/2，数字小的在前
        );

        if (scripts.isEmpty()) {
            return Collections.emptyList();
        }

        // 2. 转成 VO
        List<ScriptVO> result = new ArrayList<>(scripts.size());
        for (Script script : scripts) {
            ScriptVO vo = new ScriptVO();
            BeanUtils.copyProperties(script, vo);
            result.add(vo);
        }

        return result;
    }
}
